﻿using CPPEI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CMS.Domain.Services
{
    public class QueryService
    {
        public PagedResult<Entities.EArticle> QueryArticles(int start,int limit)
        {
            using (CMSDbContext dbContext = new CMSDbContext())
            {
                EntityRepository<Entities.EArticle> rep = new EntityRepository<Entities.EArticle>(dbContext);

                List<Entities.EArticle> list = rep.Query()
                    .OrderBy(p => p.CreatedTime)
                    .Skip(start).Take(limit).ToList();

                int total = rep.Count();

                return new PagedResult<Entities.EArticle>(list, start, limit, total);
            }
        }
        public PagedResult<Entities.EArticle> QueryPublishedTop10Articles(int start, int limit)
        {
            using (CMSDbContext dbContext = new CMSDbContext())
            {
                EntityRepository<Entities.EArticle> rep = new EntityRepository<Entities.EArticle>(dbContext);

                List<Entities.EArticle> list = rep.Query(p => p.DisplayPublishedTime != null)
                    .OrderBy(p => p.DisplayPublishedTime)
                    .Skip(start).Take(limit).ToList();

                int total = rep.Count(p => p.DisplayPublishedTime != null);

                return new PagedResult<Entities.EArticle>(list, start, limit, total);
            }
        }

        public PagedResult<Entities.EMedia> QueryMedias(Guid folderId, int start, int limit)
        {
            using (CMSDbContext dbContext = new CMSDbContext())
            {
                EntityRepository<Entities.EMedia> rep = new EntityRepository<Entities.EMedia>(dbContext);

                List<Entities.EMedia> list = rep.Query(p=>p.FolderId == folderId)
                    .OrderBy(p => p.CreatedTime)
                    .Skip(start).Take(limit).ToList();

                int total = rep.Query(p => p.FolderId == folderId).Count();

                return new PagedResult<Entities.EMedia>(list, start, limit, total);
            }
        }
    }
}
